<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
exi('','','core/member.xunibi//isrecharge=1&type='.$_RQ['type']);
$return=array();
$return['ischecklogin']=cfc('core')->checklogin('','',true);
if($_RQ['op']=='get'){
	$type=$_RQ['type'];
	if(!$type){
		exi('请选择要充值的虚拟币！','error');
	}
	exi('','','core/member.xunibi//isrecharge=1&type='.$_RQ['type']);
	$setting=uni_setting('pay');
	$credit_types=credit_types();
	$credit_title=$credit_types[$type];
	$apiplugin=$setting['credit'.$type.'_api'];
	if(!$apiplugin){
		exi($credit_title.'暂不支持充值！','error');
	}
	$return['item']=pdo_get('core_members',array('id'=>$_SESSION['uid']),array('id','nickname','credit2','credit3','credit4','credit5','credit6'));
	$return['nowcredit']=$return['item']['credit'.$type];
	$return['credit_title']=$credit_title;
	$return['post']=array('type'=>$type,'name'=>'fee','rate'=>$setting['credit'.$type.'_apirate']?:1);
	exi($return);
}
if($_RQ['op']=='recharge'){
	$post=get_postdata();
	if(intval($post['jifen'])<=0){
		exi('请先设置额度！并且为整数！','error');
	}
	$setting=uni_setting('pay');
	$jifen_rate=$setting['credit'.$post['type'].'_apirate']?:1;
	$jifen_rate=intval($jifen_rate*100)/100;
	$fee=$post['jifen']/$jifen_rate;
	$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'couponid'=>intval($post['couponid']),
		'fee'=>$fee,
		'credit'=>$post['jifen'],
		'type'=>$post['type'],
		'createtime'=>TIMESTAMP
	);
	pdo_insert('core_recharge',$data);
	$id=pdo_insertid();
	$params=array(
		'action'=>'recharge',
		'id'=>$id,
		'fee'=>$fee,
		'title'=>'币充值',
		'plugin'=>'core',
		//'pay_type'=>'wechat',
		'no_type'=>array('hdfk'),
		'back_url'=>'core/member.xunichong/pay_result',
		'success_url'=>'core/member.xunichong//type='.$post['type'],
		'error_url'=>'core/member.xunichong//type='.$post['type'],
	);
	if($_SESSION['container']=='aliapp' && $_SESSION['aliappopenid']){
		$params['pay_type']='alipay';
	}
	$pay_id=cfc('pay')->create_pay_order($params);
	exi($pay_id);
}
if($_RQ['op']=='pay_result'){
	$payresult=$_SESSION['pay_result'];
	$recharge=pdo_get('core_recharge',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	if($recharge['fee']!=$payresult['fee']){
		exi('支付失败，支付金额不匹配！','error');
	}
	if($recharge['status']){
		exi('该订单已支付！无需重复支付！','error');
	}
	if($payresult['status']!=1){
		exi('充值失败！','error');
	}
	pdo_update('core_recharge',array('status'=>1,'pay_type'=>$payresult['pay_type']),array('id'=>$recharge['id'],'uniacid'=>$_SESSION['uniacid']));
	cfc('core')->member_credit($_SESSION['uid'],'credit'.$recharge['type'],$recharge['credit'],1,'member','会员自助充值');
	exi('充值成功！');
}